from django import forms
from skran.models import History
import datetime
from django.forms.widgets import Textarea
    
class HistoryForm(forms.ModelForm):        
    check = forms.BooleanField(widget=forms.CheckboxInput(), required=False)
    
    class Meta:
        model = History
        fields = ('name', 'description', 'estimate', 'tags')
        
        widgets = {
            'description': Textarea(attrs={'cols': 30, 'rows': 5}),
        }
    
    def __init__(self, *args, **kwargs):
        super(HistoryForm, self).__init__(*args, **kwargs)        
            
    def delete(self):
        history = self.instance
        history.delete()
                          
    #Save task using form values
    def save(self, user, project, sprint, commit=True):      
        history = super(HistoryForm, self).save(commit=False)

        history.created_by = user.username
        history.updated_by = user.username
        history.create_date = datetime.datetime.now()
        history.last_update = datetime.datetime.now()
        
        history.sprint = sprint
        history.project = project
        
        
        if commit:
            history.save()
        return history
